package com.cusprovider.service;


import com.alibaba.dubbo.config.annotation.Service;
import com.cusprovider.dao.AuthorityDao;
import com.pojo.Authority;
import com.service.AuthorityService;
import com.service.UserService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Component
@Transactional
@Service(interfaceClass = AuthorityService.class)
public class AuthorityServiceImpl implements AuthorityService {

    @Resource
    private AuthorityDao authorityDao ;

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int addAuthority(int rid, String functionIds) {
        int success = 1 ;

        //先删除角色原先的权限
        authorityDao.deleteAuthorityByRole(rid) ;

        //再重新为角色添加权限
        for(String fid : functionIds.split(",")) {
            Authority authority = new Authority() ;
            authority.setRoleId(rid);
            authority.setFunctionId(Integer.parseInt(fid));

            authorityDao.addAuthority(authority) ;
        }

        return success ;
    }
}
